<template>
  <div class="toc-content">
    <ul class="toc-items">
      <li v-for="{ id, name, children } in headers" :key="id" class="toc-item">
        <p :class="id === url ? 'active' : ''" @click="navChange(id)">
          {{ name }}
          <span ref="marker" class="toc-marker" v-if="id === url" />
        </p>
        <ul v-if="children">
          <li v-for="{ id, name } in children" :key="id" class="toc-item">
            <p class="p-style" :class="id === url ? 'active' : ''" @click="navChange(id)">
              {{ name }}
              <span ref="marker" class="toc-marker" v-if="id === url" />
            </p>
          </li>
        </ul>
      </li>
    </ul>
  </div>
</template>

<script lang="ts">
import { defineComponent, toRefs, reactive, watch } from 'vue'

export default defineComponent({
  name: 'rightNav',
  components: {},
  props: {
    headers: {
      type: Array,
      default: () => []
    },
    toId: {
      type: String,
      default: ''
    }
  },
  // ----------------生命周期-----------------------
  setup(props, { emit }) {
    const state = reactive({
      url: 'basicInformation'
    })
    const navChange = (id: any) => {
      state.url = id
      emit('onScrollNav', id)
    }
    watch(
      () => props.toId,
      () => {
        state.url = props.toId
      },
      { immediate: true }
    )
    return {
      ...toRefs(state),
      navChange
    }
  }
})
</script>

<style lang="stylus" scoped>
.toc-content {
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    width: 100%;
    height: 100%;
    box-shadow: inset 2px 0px 0px var(--mc-gray4-color);
    flex: none;
    order: 1;
    flex-grow: 0;
    font-size: 14px;
    line-height: 20px;
    color: rgba(0, 0, 0, 0.88)

    .toc-items {
      list-style: none;
      padding: 0;
      margin: 0;
      line-height: 1.2;
      display flex
      flex-direction column
      gap:16px
      .toc-item {
        cursor pointer
        ul {
            padding 0
            font-size: 12px;
            line-height: 18px;
            letter-spacing: 0.001em;
            color: rgba(0, 0, 0, 0.6);
            .p-style{
                padding: 0 0 0 32px;
            }
        }
        text-overflow: ellipsis;
        overflow: hidden;
        white-space: nowrap;
        p {
            padding: 0 0 0 16px;
            margin 0
            height 20px
            line-height 20px
        }
      }
      .active{
        color var(--mc-main-color)
        position: relative;
      }
      .toc-marker {
        display inline-block
        position: absolute;
        background-color: var(--mc-main-color);
        border-radius: 4px;
        width: 2px;
        height: 20px;
        top: 0;
        left: 0;
        z-index: 0;
      }
      .toc-item:last-child {
        margin-bottom 0
      }
    }
  }
</style>
